<?php
/*
Plugin Name: FeedMe
Plugin URI: http://myfla.ws
Description: Integrated FeedBurner support for WordPress
Author: Arthus Erea
Version: 1.0
Author URI: http://myfla.ws
*/

/**
 * ae_feedme_display_img
 *
 * This will output an image chicklet
 *
 * @param string (optional) - Background color for chicklet
 * @param string (optional) - Foreground color for chicklet
 *
 * @return string - A chicklet image wrapped in a link to feed
 */
function ae_feedme_display_img($bg = '',$fg = '') {
	if(empty($bg)) {
		$bg = get_option('ae_feedme_bg');
		}
	if(empty($fg)) {
		$fg = get_option('ae_feedme_fg');
		}
	$bg = str_replace('#','',$bg);
	$fg = str_replace('#','',$fg);
	$output = '<a href="'.get_option('ae_feedme_uri').'">';
	$output = $output.'<img src="http://feeds.feedburner.com/~fc/'.get_option('ae_feedme_name').'?bg='.$bg.'&amp;fg='.$fg.'&amp;anim=0" height="26" width="88" style="border:0" alt="" /></a>';
	return $output;
	}
	
// ae_feedme_add_pages() is the sink function for the 'admin_menu' hook
function ae_feedme_add_pages() {
    // Add a new top-level menu (ill-advised):
    add_menu_page('FeedMe', 'Feed Data', 2, __FILE__, 'ae_feedme_default_page');
	
	// Add a submenu to the custom top-level menu:
    add_submenu_page(__FILE__, 'FeedMe Setup', 'Setup', 10, 'feed/options','ae_feedme_options_page');
}

// ae_feedme_default_page() displays the page content for the default submenu\
function ae_feedme_default_page() { ?>
	<div class="wrap">
		<h2>FeedMe</h2>
		<p>FeedMe provides seamless and easy integration of WordPress with <a href="http://feedburner.com">FeedBurner</a>. It is released under a 
		<a href="http://en.wikisource.org/wiki/GNU_GPL">GNU General Public License</a> by <a href="http://myfla.ws">Arthus Erea</a>.
		</p>
		<h3>Chicklet</h3>
		<ul>
			<li>Requires FeedCount to be enabled</li>
		</ul>
		<code><?php echo htmlentities('<?php echo ae_feedme_display_img(); ?>'); ?></code>
	</div>
<?php }

function ae_feedme_feed_redirect() {
	global $wp, $feed;
	if (is_feed() && $feed != 'comments-rss2' && !is_single()) {
		if (function_exists('status_header')) status_header( 307 );
		header('Location: http://feeds.feedburner.com/' . get_option('ae_feedme_name'));
		header("HTTP/1.1 307 Temporary Redirect");
		exit();
	} elseif (is_feed() && ($feed == 'comments-rss2') && (get_option('ae_feedme_cmtname'))) {
		if (function_exists('status_header')) status_header( 307 );
		header('Location: http://feeds.feedburner.com/' . get_option('ae_feedme_cmtname'));
		header("HTTP/1.1 307 Temporary Redirect");
		exit();
	}
}

function ae_feedme_check_url() {
	switch (basename($_SERVER['PHP_SELF'])) {
		case 'wp-rss.php':
		case 'wp-rss2.php':
		case 'wp-atom.php':
		case 'wp-rdf.php':
			if (get_option('ae_feedme_name') != '') {
				if (function_exists('status_header')) status_header( 307 );
				header('Location: http://feeds.feedburner.com/' . get_option('ae_feedme_name'));
				header("HTTP/1.1 307 Temporary Redirect");
				exit();
			}
			break;
		case 'wp-commentsrss2.php':
			if (get_option('ae_feedme_cmtname') != '') {
				if (function_exists('status_header')) status_header( 307 );
				header('Location: http://feeds.feedburner.com/' . get_option('ae_feedme_cmtname'));
				header("HTTP/1.1 307 Temporary Redirect");
				exit();
			}
			break;
	}
}
// ae_feedme_options_page() displays the page content for the Options submenu
function ae_feedme_options_page() { ?>
	<div class="wrap">
	<?php
	// Reset
	if(isset($_POST['ae_feedme_delete'])) {
		delete_option('ae_feedme_uri');
		delete_option('ae_feedme_cmturi');
		delete_option('ae_feedme_name');
		delete_option('ae_feedme_cmtname');
		delete_option('ae_feedme_bg');
		delete_option('ae_feedme_fg');
		echo '<div class="updated"><p><strong>All plugin data cleared.</strong></p></div>';
		}
	// Installation
	if(get_option('ae_feedme_uri') == '' && !isset($_POST['ae_feedme_set_name'])) { ?>
		<h2>FeedMe Setup</h2>
		<p>FeedMe can be setup very easily. You simply need to point it to your FeedBurner feed. For your convenience, the process has been divided into three easy steps.</p>
		<form id="ae_feedme_set" method="post">
		<input type="hidden" id="ae_feedme_set" name="ae_feedme_set" value="setup" />
			<ol>
				<li>If you have don't have a FeedBurner feed yet, you need to <a href="https://www.feedburner.com/fb/a/addfeed?sourceUrl=<?php echo get_bloginfo('rss2_url'); ?>" title="Create your FeedBurner feed">create one</a>.</li>
				<li>Enter the <q>Feed Address</q> you got from FeedBurner:
					<label for="ae_feedme_set_name">http://feeds.feedburner.com/</label> <input style="width:150px" ""type="text" name="ae_feedme_set_name" id="ae_feedme_set_name" value="<?php echo str_replace(' ','',get_bloginfo('name')); ?>" />
				</li>
				<li><em>Optional</em>: If you would like FeedBurner to handle your comments, create a <a href="https://www.feedburner.com/fb/a/addfeed?sourceUrl=<?php echo get_bloginfo('comments_rss2_url'); ?>" title="Create your FeedBurner feed">comments feed</a> and enter it here: 
				<label for="ae_feedme_set_cmtname">http://feeds.feedburner.com/</label> <input style="width:200px" type="text" name="ae_feedme_set_cmtname" id="ae_feedme_set_cmtname" value="" />
				</li>
			</ol>
			<input type="submit" id="ae_feedme_set_submit" value="Setup" />
		</form>
		<?php
		} else {
		if(isset($_POST['ae_feedme_set_name'])) {
			add_option('ae_feedme_name', $_POST['ae_feedme_set_name'], 'FeedMe main feed name');
			add_option('ae_feedme_uri', 'http://feeds.feedburner.com/'.$_POST['ae_feedme_set_name'], 'FeedMe main feed URI');
			add_option('ae_feedme_bg', '#99CCFF', 'FeedMe default chicklet background');
			add_option('ae_feedme_fg', '#444444', 'FeedMe default chicklet background');
			if(isset($_POST['ae_feedme_cmtname'])) {
				add_option('ae_feedme_cmtname', $_POST['ae_feedme_set_cmtname'], 'FeedMe comments feed name');
				add_option('ae_feedme_cmturi', 'http://feeds.feedburner.com/'.$_POST['ae_feedme_set_cmtname'], 'FeedMe comments feed URI');
				echo '<div class="updated"><p><strong>Set main feed and comments feed.</strong></p></div>';
				} else {
				echo '<div class="updated"><p><strong>Set main feed.</strong></p></div>';
				}
			}
	if (isset($_POST['feedme_options'])) {	
		if(!empty($_POST['ae_feedme_name'])) {
			update_option('ae_feedme_name', $_POST['ae_feedme_name']);
			update_option('ae_feedme_uri', 'http://feeds.feedburner.com/'.$_POST['ae_feedme_name']);
			}
		if(!empty($_POST['ae_feedme_bg'])) {
			update_option('ae_feedme_bg', $_POST['ae_feedme_bg']);
			}
		if(!empty($_POST['ae_feedme_fg'])) {
			update_option('ae_feedme_fg', $_POST['ae_feedme_fg']);
			}
		echo '<div class="updated"><p><strong>FeedMe options updated</strong></p></div>';
		} ?>
	 <h2>FeedMe Options</h2>
  <form method="post">
	<input type="hidden" name="feedme_options" id="feedme_options" value="set" />
	<label for="ae_feedme_name">Feed Name:</label> http://feeds.feedbuner.com/<input type="text" id="ae_feedme_name" name="ae_feedme_name" value="<?php echo get_option('ae_feedme_name'); ?>" /><br />
	<label for="ae_feedme_bg">Default Chicklet Background</label> <input type="text" id="ae_feedme_bg" name="ae_feedme_bg" value="<?php echo get_option('ae_feedme_bg'); ?>" /><br />
	<label for="ae_feedme_fg">Default Chicklet Foreground</label> <input type="text" id="ae_feedme_fg" name="ae_feedme_fg" value="<?php echo get_option('ae_feedme_fg'); ?>" /><br />
	<input type="submit" id="feedme_submit" value="Update Plugin &raquo;" />
	<input type="submit" id="ae_feedme_delete" name="ae_feedme_delete" value="&laquo; Reset Plugin" />
  </form>
<?php } ?>
</div>
<?php }

// Redirect feed
if (!preg_match("/feedburner/i", $_SERVER['HTTP_USER_AGENT'])) {
	add_action('template_redirect', 'ae_feedme_feed_redirect');
	add_action('init','ae_feedme_check_url');
}
// Insert the ae_feedme_add_pages() sink into the plugin hook list for 'admin_menu'
add_action('admin_menu', 'ae_feedme_add_pages');
?>